GBML manual / Grids

grid_shuffle

Mixes up the cells of target, in place: afterwards every cell holds one of the values the grid held before, in a random place. Cells move one at a time, so a row or a column does not stay together (use grid_shuffle_rows or grid_shuffle_columns for that). It makes no copy.

Syntax

grid_shuffle(target)

Arguments

NameType
targetgrid

Returns

Nothing.

Description

Mixes up the cells of target, a grid. Afterwards every cell holds one of the values the grid held before, in a random place. Every cell is moved on its own, so a row or a column does not stay together.

grid_shuffle(board);

There are two more shuffles for a grid that keep things together. grid_shuffle_rows moves whole rows and grid_shuffle_columns moves whole columns.

You wantUse
every cell in a random placegrid_shuffle
the rows in a random order, each row staying as it isgrid_shuffle_rows
the columns in a random order, each column staying as it isgrid_shuffle_columns

It changes the grid itself

grid_shuffle moves the values that are already in target. It does not make a new grid, and it needs no extra memory. The old arrangement is gone afterwards. If you want to keep it, make a copy with grid_copy first and shuffle the one you want mixed up.

The grid keeps its width and height. Any kind of value can be shuffled: numbers, text, structs, references. Nothing is compared, so there is no rule about what the grid holds. (A sort is different: it only works on numbers and text.)

Where the random numbers come from

A shuffle picks its random numbers from the same place roll_int does: one stream of random numbers for the whole game. There is no seed to give the shuffle itself. What follows from that:

Every possible arrangement can come up. It is very slightly more likely to land on some than others, because a random number is cut down to fit a small range, but the difference is far too small for a game to show.

What happens in the awkward cases

You do thisWhat happens
shuffle a grid with one cellnothing changes, and no random numbers are used
shuffle a grid that has the same value more than onceworks as normal. Equal values are interchangeable, so you cannot tell which is which
shuffle and hope a cell keeps its valueit may. That can happen with random order, and it gets less likely the more cells there are
give it a list or an arrayrefused when checked, and the message names the function that shuffles that kind
use it where a value is neededrefused when checked. It gives nothing back

In a script

A script that takes a grid as a parameter works on the caller's own grid (see Grids). grid_shuffle needs no width or height, because it moves cells one at a time, so unlike grid_shuffle_rows it works inside a script, on the caller's grid, whatever size that is.

What it costs

Nothing is set aside: the cells are swapped where they stand, one swap for each cell after the first. No timing was measured.

Example

var board = grid_create(3, 3, 0);
board[# 0, 0] = 1;
board[# 1, 1] = 2;
board[# 2, 2] = 3;
grid_shuffle(board);     // the 1, the 2, the 3 and six 0s, in new places

On the GBA

Shuffles every cell of the vector as one long run, row by row. It needs no width or height, so, like grid_fill, it also works inside a script on the CALLER's grid, whatever size that is. A Fisher-Yates shuffle done in place: it makes no copy and needs no extra memory. For each place from the last down to the second, it swaps that place with one picked at random from the places up to and including it, so every order can come up. The pick comes from gbml::roll_int, the one stream the roll_ functions use, so a shuffle follows the same rules: the same ROM shuffles the same way every time it is switched on, roll_reseed changes that, and roll_reseed with the same number just before a shuffle gives the same shuffle again. A shuffle uses up rolls from that stream (one for each place after the first), so it moves every later roll along. Nothing is compared, so any kind of item can be shuffled.

See also: grid_shuffle_rowsgrid_shuffle_columnslist_shufflearray_shufflegrid_sortgrid_fillgrid_copyroll_introll_reseedGridsData containers